package com.itheima.transaction;

import com.itheima.utils.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Component
public class TransactionManager {
    @Autowired
    private DataSource dataSource;
    public void start() throws SQLException {
        Connection connection = DataSourceUtil.getConnection(dataSource);
        connection.setAutoCommit(false);

    }

    public void commit() {
        try {
            Connection connection = DataSourceUtil.getConnection(dataSource);

            connection.commit();
        }catch (Exception e){
            //do nothing~~~~
        }

    }

    public void rollback(){
        try {
            Connection connection = DataSourceUtil.getConnection(dataSource);

            connection.rollback();
        }catch (Exception e){
            //do nothing~~~~
        }
    }


}
